/**
 * 文件：ModifyStuPanel.java
 * 说明：用于查询学生
 * 作者：古通基
 * 时间：1211-09-13
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ModifyStuPanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = -2938426104772267504L;
	private JLabel lno = new JLabel("学号：");                                                //静态标签
	private JLabel lname = new JLabel("姓名：");
	private JLabel lsex = new JLabel("性别：");             
	private JLabel lnation = new JLabel("民族：");     
	private JLabel lcollege = new JLabel("院系：");       
	private JLabel lspecialty = new JLabel("专业：");    
	private JLabel lbirthday = new JLabel("生日：");     
	private JLabel lgrade = new JLabel("年级：");       
	
	private JTextField no = new JTextField("请输入五位学号",10);                    //文本输入
	private JTextField name = new JTextField("",10); 
	private JTextField birthday = new JTextField("",10);
	
    String[] sexs = {"男","女"};                                                                         //选择列表
    private JComboBox sex = new JComboBox(sexs);    
    String[] nations = {"汉族","壮族","苗族","黎族","满族","瑶族","藏族","蒙族","其他"};
    private JComboBox nation = new JComboBox(nations);    
    String[] colleges = {"信息工程学院","外国语学院","食品工程学院","海洋生命学院","数学科学学院"};
    private JComboBox college = new JComboBox(colleges);    
    String[]  specialties= {"计算机","朝鲜语","营养科学","生物科学","应用数学"};
    private JComboBox specialty = new JComboBox(specialties);    
    String[]  grades= {"2008","2009","2010","2011"};
    private JComboBox grade = new JComboBox(grades);   
	
    private JButton query = new JButton("查询");                            //声明按钮
    private JButton ok = new JButton("确定");
    private JButton modify = new JButton("修改");
    	
    private JPanel JPcomon1 = new JPanel();                                   //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPcomon6 = new JPanel();
    private JPanel JPcomon7 = new JPanel();
    private JPanel JPcomon8 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public ModifyStuPanel()                                                             //构造函数
    {
    	lno.setHorizontalAlignment(SwingConstants.CENTER);
    	lname.setHorizontalAlignment(SwingConstants.CENTER);
    	lsex.setHorizontalAlignment(SwingConstants.CENTER);
    	lcollege.setHorizontalAlignment(SwingConstants.CENTER);
    	lspecialty.setHorizontalAlignment(SwingConstants.CENTER);
    	lgrade.setHorizontalAlignment(SwingConstants.CENTER);
    	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(lno);
    	JPcomon1.add(no);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lsex);
    	JPcomon3.add(sex);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lnation);
    	JPcomon4.add(nation);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(lcollege);
    	JPcomon5.add(college);
    	JPcomon6.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon6.add(lspecialty);
    	JPcomon6.add(specialty);
    	JPcomon7.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon7.add(lbirthday);
    	JPcomon7.add(birthday);
    	JPcomon8.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon8.add(lgrade);
    	JPcomon8.add(grade);
    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(modify);
    	JPbutton.add(ok);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPcomon6);
    	this.add(JPcomon7);
    	this.add(JPcomon8);
    	this.add(JPbutton);
    	
		name.setEditable(false);
		sex.setEnabled(false);
		nation.setEnabled(false);
		college.setEnabled(false);
		specialty.setEnabled(false);
		birthday.setEditable(false);
		grade.setEnabled(false);    
    	
    	query.addActionListener(this);                                                        //事件监听        
    	ok.addActionListener(this);
    	modify.addActionListener(this);
    	no.addMouseListener(this);
    }
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
			query.setEnabled(false);
		}
		else if(e.getActionCommand().equals("修改"))
		{
			Modify();
		}
		else if(e.getActionCommand().equals("确定"))
		{
			Ok();
			no.setEditable(true);			
			name.setEditable(false);
			sex.setEnabled(false);
			nation.setEnabled(false);
			college.setEnabled(false);
			specialty.setEnabled(false);
			birthday.setEditable(false);
			grade.setEnabled(false);  
			query.setEnabled(true);
		}
	}	
	
	private void QueryDataBase()
	{
		ResultSet rs = null;
		try
		{
			BaseSQL stu = null;
			stu = new BaseSQL();	
			String sql = "select * from student where sno = '"+Integer.parseInt(no.getText())+"'";  
			
		    rs = stu.executeSelect(sql);
		    if(rs.next())
    		{
    			if(Integer.parseInt(no.getText())==rs.getInt("sno"))
    			{
    				name.setText(rs.getString("sname"));
    				birthday.setText(rs.getString("birthday"));
    				sex.setSelectedItem(rs.getString("sex"));
    				nation.setSelectedItem(rs.getString("nation"));
    				college.setSelectedItem(rs.getString("college"));
    				specialty.setSelectedItem(rs.getString("specialty"));    				
    				grade.setSelectedItem(rs.getString("grade"));	    				
        		}    				
       		} 
    		else
    		{
    			JOptionPane.showMessageDialog(null,"不存在学号为："+no.getText()+" 的学生","查询错误",JOptionPane.INFORMATION_MESSAGE);
    		}
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	private void Modify()
	{
		no.setEditable(false);
		name.setEditable(true);
		sex.setEnabled(true);
		nation.setEnabled(true);
		college.setEnabled(true);
		specialty.setEnabled(true);
		birthday.setEditable(true);
		grade.setEnabled(true);
	}
	
	private void Ok()
	{
		try
		{
			BaseSQL stu = null;
			stu = new BaseSQL();
			String sql = "update student" +
					" set sname='"+name.getText()+"',sex='"+sex.getSelectedItem()+"',nation= '"+nation.getSelectedItem()+"',college='"+college.getSelectedItem()+"',specialty= '"+specialty.getSelectedItem()+"',birthday='"+birthday.getText()+"',grade='"+ grade.getSelectedItem()+"' where sno = '"+Integer.parseInt(no.getText())+"'";
			stu.executeDML(sql);
		    JOptionPane.showMessageDialog(this, "修改成功", "修改学生信息",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}	
	
    public void mouseClicked(MouseEvent e) { }                                           //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入五位学号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}    	
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}

